package com.shaoshaohuo.app.ui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.HomeTabLayoutAdapter;

import java.util.ArrayList;
import java.util.List;

public class MyFarmOrderFragment extends Fragment {
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    private String mParam1;
    private String mParam2;
    private View view;
    private TabLayout my_farm_order_tabLayout;
    private List<String> tab_data;
    private List<Fragment> fragment_data;
    private FarmOrderStaticFragment farmStaticFragment;
    private ViewPager my_farm_order_viewPager;
    private HomeTabLayoutAdapter adapter;

    public MyFarmOrderFragment() {
        // Required empty public constructor
    }
    public static MyFarmOrderFragment newInstance(String param1, String param2) {
        MyFarmOrderFragment fragment = new MyFarmOrderFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my_farm_order, container, false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView();
        getData();
        bindAdapter();
    }

    private void bindAdapter() {
        my_farm_order_viewPager.setAdapter(adapter);
        my_farm_order_tabLayout.setupWithViewPager(my_farm_order_viewPager);
    }

    private void getData() {
        tab_data.add("全部");
        tab_data.add("待确认");
        tab_data.add("已完成");
        tab_data.add("评价");
        Bundle bundle;
        for (int i = 0; i < tab_data.size(); i++) {
            bundle = new Bundle();
            farmStaticFragment = new FarmOrderStaticFragment();
            my_farm_order_tabLayout.addTab(my_farm_order_tabLayout.newTab().setText(tab_data.get(i)));
            farmStaticFragment = new FarmOrderStaticFragment();
            fragment_data.add(farmStaticFragment);
            bundle.putString("type", tab_data.get(i));
            farmStaticFragment.setArguments(bundle);
        }
    }

    private void initView() {
        my_farm_order_tabLayout = (TabLayout) view.findViewById(R.id.my_farm_order_tabLayout);
        tab_data = new ArrayList<>();
        my_farm_order_viewPager = (ViewPager) view.findViewById(R.id.my_farm_order_viewPager);
        fragment_data = new ArrayList<>();
        adapter = new HomeTabLayoutAdapter(getChildFragmentManager(), getActivity(), tab_data, fragment_data);
    }


}
